'use strict'

let home_fn = async (ctx, next) => {
    ctx.body =
        `
    <form action="/login" method="POST">
        <div>
            <label for="">账户</label>
            <input type="text" name="username">
        </div>
        <div>
            <label for="">密码</label>
            <input type="password" name="password">
        </div>
        <div>
            <input type="submit" value="登录">
        </div>
    </form>
    `
}

let user_fn = async (ctx, next) => {
    let username = ctx.request.body.username || '';
    let password = ctx.request.body.password || '';

    if (username === 'admin' && password === '123') {
        ctx.response.redirect('/login/win');
    } else {
        ctx.response.redirect('/login/error');
    }
}

let win_fn = async (ctx, next) => {
    ctx.body = '<h1>登录成功</h1>'
}

let error_fn = async (ctx, next) => {
    ctx.body = '<h1>登录失败</h1>'
}

module.exports = {
    '/': ['get', home_fn],
    '/login': ['post', user_fn],
    '/login/win': ['get', win_fn],
    '/login/error': ['get', error_fn],
}